package com.example.databasehomework;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhouyou.recyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class Activity_ExpInfo extends AppCompatActivity {

    public static final int DETAIL = 0, WAITINGLIST = 1;

    private TextView head_ExpName, head_ExpNum;
    private ImageView head_img;
    private ConstraintLayout conl_detail, conl_queue;
    private List<DetailListBean> detailData;
    private DetailListAdapter detailListAdapter;
    private XRecyclerView expInfoView;
    private String expId, roomId, expName;
    public int status;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exp_info);
        status = DETAIL;
        getItems();
        setDetailLayout();
        setHeadImgListener();
        setBottomListener();
        initSignInViewContent();
        setItemListener();
        getStudents();
    }

    public void getItems() {
        this.head_ExpName = findViewById(R.id.exp_info_head_text_exp);
        this.head_ExpNum = findViewById(R.id.exp_info_head_text_expinfo);
        this.head_img = findViewById(R.id.exp_info_head_img);
        this.conl_detail = findViewById(R.id.exp_info_bottom_detail);
        this.conl_queue = findViewById(R.id.exp_info_bottom_qa);
        this.expInfoView = findViewById(R.id.exp_info_body_rview);
        Intent intent = getIntent();
        expName = intent.getStringExtra("expName");
        expId = intent.getStringExtra("expId");
        roomId = intent.getStringExtra("roomId");
        head_ExpName.setText(expName);
        head_ExpNum.setText(expId);
        detailData = new ArrayList<>();
    }

    public void setHeadImgListener() {
        this.head_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void setBottomListener() {
        this.conl_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                status = DETAIL;
                getStudents();
            }
        });
        this.conl_queue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                status = WAITINGLIST;
                getQueue();
            }
        });
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    public void setDetailLayout() {
        conl_detail.setBackground(getResources().getDrawable(R.drawable.circle_top_bg));
        conl_queue.setBackground(getResources().getDrawable(R.drawable.button_press));
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    public void setQueueLayout() {
        conl_detail.setBackground(getResources().getDrawable(R.drawable.button_press));
        conl_queue.setBackground(getResources().getDrawable(R.drawable.circle_top_bg));
    }

    private void initSignInViewContent() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        detailListAdapter = new DetailListAdapter(this, detailData, this);
        expInfoView.setPullRefreshEnabled(true);
        expInfoView.setLoadingMoreEnabled(false);
        expInfoView.setLayoutManager(new LinearLayoutManager(this));
        expInfoView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        setDetailListListener();
        expInfoView.setAdapter(detailListAdapter);
    }

    public void setDetailListListener() {
        expInfoView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (status == DETAIL) {
                            detailData = HttpUnit.getStudentFromExp(expId, roomId);
                        } else {
                            detailData = HttpUnit.getStudentWaitingList(roomId, expId);
                        }
                        runOnUiThread(new Runnable() {
                            @SuppressLint("NotifyDataSetChanged")
                            @Override
                            public void run() {
                                if (detailData == null) {
                                    Toast.makeText(getBaseContext(), "获取学生列表失败", Toast.LENGTH_SHORT).show();
                                    expInfoView.refreshComplete();
                                } else {
                                    detailListAdapter.data = detailData;
                                    detailListAdapter.notifyDataSetChanged();
                                    expInfoView.refreshComplete();
                                }
                            }
                        });
                    }
                }).start();
            }
            @Override
            public void onLoadMore() {}
        });
    }

    public void setItemListener() {
        detailListAdapter.setOnItemClickListener(new DetailListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(Activity_ExpInfo.this, Activity_Student_Detail.class);
                intent.putExtra("seatNum", detailData.get(position - 1).seatNum);
                intent.putExtra("expId", expId);
                intent.putExtra("stuNum", detailData.get(position - 1).sNumber);
                intent.putExtra("stuName", detailData.get(position - 1).name);
                Toast.makeText(getBaseContext(),
                        "开始问答 " + detailData.get(position - 1).name + " #" + detailData.get(position - 1).sNumber, Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
        });
    }

    public void getStudents() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                detailData = HttpUnit.getStudentFromExp(expId, roomId);
                runOnUiThread(new Runnable() {
                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    public void run() {
                        if (detailData == null) {
                            Toast.makeText(getBaseContext(), "获取学生信息失败", Toast.LENGTH_SHORT).show();
                        } else {
                            setDetailListListener();
                            detailListAdapter.data = detailData;
                            detailListAdapter.notifyDataSetChanged();
                            setDetailLayout();
                        }
                    }
                });
            }
        }).start();
    }

    public void signIn(String stuNum, boolean ifSignIn) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean success = HttpUnit.modifyStudentExpSignInInfo(expId, stuNum, ifSignIn);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (success) {
                            if (status == DETAIL) {
                                getStudents();
                            } else {
                                getQueue();
                            }
                        } else {
                            Toast.makeText(getBaseContext(), "签到失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }).start();
    }

    public void getQueue() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                detailData = HttpUnit.getStudentWaitingList(roomId, expId);
                runOnUiThread(new Runnable() {
                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    public void run() {
                        if (detailData == null) {
                            Toast.makeText(getBaseContext(), "获取排队列表失败", Toast.LENGTH_SHORT).show();
                        } else {
                            setDetailListListener();
                            detailListAdapter.data = detailData;
                            detailListAdapter.notifyDataSetChanged();
                            setQueueLayout();
                        }
                    }
                });
            }
        }).start();
    }

    @Override
    protected void onRestart() {
        if (status == DETAIL) {
            getStudents();
        } else {
            getQueue();
        }
        super.onRestart();
    }
}